//
//  NSDictionaryAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "NSDictionaryAdditions.h"


@implementation NSDictionary (Tint)



#pragma mark - Existence check

- (BOOL)hasObjectForKey:(id)key {
	return [[self allKeys] containsObject:key];
}



#pragma mark - Accessing values

- (CGPoint)pointForKey:(NSString *)key {
    CGPoint point = CGPointZero;
    NSDictionary *dictionary = [self valueForKey:key];
    if (CGPointMakeWithDictionaryRepresentation((CFDictionaryRef)dictionary, &point)) {
        return point;
    }
    return CGPointZero;
}

- (CGSize)sizeForKey:(NSString *)key {
    CGSize size = CGSizeZero;
    NSDictionary *dictionary = [self valueForKey:key];
    if (CGSizeMakeWithDictionaryRepresentation((CFDictionaryRef)dictionary, &size)) {
        return size;
    }
    return CGSizeZero;
}

- (CGRect)rectForKey:(NSString *)key {
    CGRect rect = CGRectZero;
    NSDictionary *dictionary = [self valueForKey:key];
    if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)dictionary, &rect)) {
        return rect;
    }
    return CGRectZero;
}



#pragma mark - URL

- (NSString *)queryString {
    NSMutableArray *parameters = [NSMutableArray array];
    
    NSEnumerator *enumerator = [self keyEnumerator];
    NSString *key = nil;
    while ((key = [enumerator nextObject])) {
        NSString *parameter = [self objectForKey:key];
        [parameters addObject:[NSString stringWithFormat:@"%@=%@", key, parameter]];
    }
    
    return [parameters componentsJoinedByString:@"&"];
}

@end
